<?php 

class LoginPresenter extends basePresenter{
	
	/**
	 * Start lifecycle presenter
	 *
	 * @return void
	 */
	protected function startup()
	{
		parent::startup();
		$user = Environment::getUser();
		
		$this->template->prihlasen = $user->isAuthenticated();
	}

	/**
	 * Settings for all render
	 *
	 * @return void
	 */
	protected function beforeRender()
	{
		//parent::startup();
	}

	/**
	 * Render default action
	 *
	 * @return void
	 */
	public function renderDefault()
	{
		$this->setLayout("login");
		$this->template->form = $this->getComponent('loginForm');
		$user = Environment::getUser();
		if($user->isAuthenticated()) $this->redirect("Web:");
	}
	
	public function actionLogout(){
		Environment::getUser()->signOut(TRUE);
		$this->flashMessage("Byl/a jste úspěšně odhlášen/a.", "ok");
		$this->redirect('Login:');
	}
	
	protected function createComponentLoginForm()
	{
		$form = new AppForm;

		$form->addText('login', 'Login:')
			->addRule(Form::FILLED, 'Login musí být vyplněn')
			->addCondition(Form::FILLED);
		$form["login"]->getControlPrototype()->id = "login";
			
		$form->addPassword('password', 'Password:')
			->addRule(Form::FILLED, 'Zadejte heslo');
		$form["password"]->getControlPrototype()->id = "pass";
			
		$form->addSubmit('auth', 'Přihlásit')->getControlPrototype()->class('submit');
		
		$form->onSubmit[] = array($this, 'loginFormSubmitted');

		return $form;
	}
	
	public function loginFormSubmitted(AppForm $form)
	{
		$values = $form->getValues();

			$user = Environment::getUser();
			try{
			
			$user->authenticate($values["login"], $values["password"]);
			}catch (AuthenticationException $e){
				$this->getPresenter()->flashMessage($e->getMessage(), 'warning');
			}

	}
	
	public function actionDefautlNotSet(){
		$this->setLayout("layout");
	}
	
}

?>